package com.mwm.paiwei.meiye.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mwm.paiwei.meiye.activity.CharactorSelectActivity;
import com.mwm.paiwei.meiye.model.LoginBean;
import com.mwm.paiwei.meiye.R;
import com.mwm.paiwei.meiye.activity.WebViewActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by companycn on 2017/6/12.
 */

public class CharactorSelectAdapter extends RecyclerView.Adapter<CharactorSelectViewHolder> {

    private Context mContext;
    private LayoutInflater inflater;
    private List<LoginBean.DataBean.UserTokensBean> userTokens;


    public CharactorSelectAdapter(Context mContext, List<LoginBean.DataBean.UserTokensBean> userTokens) {
        this.mContext = mContext;
        this.userTokens = userTokens;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public CharactorSelectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CharactorSelectViewHolder holder = new CharactorSelectViewHolder(inflater.inflate(R.layout.select_item, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(CharactorSelectViewHolder holder, final int position) {
        holder.itemTv.setText(userTokens.get(position).getUserTypeName());


        holder.itemTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                SharedPreferences.Editor editor = mContext.getSharedPreferences("user", Context.MODE_PRIVATE).edit();
                editor.putLong("uid_new",userTokens.get(position).getUserId());
                editor.putString("token_new",userTokens.get(position).getToken());
                editor.putString("utype_new",userTokens.get(position).getUserType()+"");
                editor.commit();

                Intent i = new Intent(mContext, WebViewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putLong("uid", userTokens.get(position).getUserId());
                bundle.putString("token", userTokens.get(position).getToken());
                bundle.putString("utype", userTokens.get(position).getUserType()+"");
//                bundle.putBoolean("isLoadNextActivity", isLoadNextActivity);
                i.putExtras(bundle);

                mContext.startActivity(i);
                ((CharactorSelectActivity)mContext).finish();
            }
        });

    }

    @Override
    public int getItemCount() {
        return userTokens.size();
    }

}

class CharactorSelectViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.item_tv)
    TextView itemTv;

    public CharactorSelectViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }
}
